package com.newzqxq.koalamoney.ui.fragment;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.newzqxq.koalamoney.R;
import com.newzqxq.koalamoney.common.BaseFragment;
import com.newzqxq.koalamoney.common.Params;
import com.newzqxq.koalamoney.entity.AutoPlanBean;
import com.newzqxq.koalamoney.ui.activity.ProductDetailsActivity;
import com.newzqxq.koalamoney.ui.adapter.AutoPlanAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

import static android.R.layout.simple_list_item_2;
import static android.support.v7.widget.DividerItemDecoration.VERTICAL;

/**
 * 定投
 * Created by ZQXQ-Developer on 2017/7/18.
 */

public class AutoPlanFragment extends BaseFragment {
    private static boolean isFirstEnter = true;
    @BindView(R.id.refreshLayout)
    RefreshLayout mRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private String mTitleCode = "";
    private List<AutoPlanBean> data = new ArrayList<>();
    AutoPlanAdapter adapter;
    private ClassicsHeader mClassicsHeader;

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.ui_auto_plan, null);
    }

    @Override
    protected void initData() {
        mTitleCode = getArguments().getString(Params.DATA);
        Log.i("resultinitData", "initData: " + mTitleCode);
        if (isFirstEnter) {
            isFirstEnter = false;
//            mRefreshLayout.autoRefresh(2000);//第一次进入触发自动刷新，演示效果
        }
        mClassicsHeader = (ClassicsHeader) mRefreshLayout.getRefreshHeader();
        mClassicsHeader.setSpinnerStyle(SpinnerStyle.Scale);

        adapter = new AutoPlanAdapter(data, getContext());
        initDtas();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                initDtas();
                refreshlayout.finishRefresh(2000);
            }
        });
        mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                initDta2();
                adapter.notifyDataSetChanged();
                mRefreshLayout.finishLoadmore();
            }
        });
        adapter.setmItemClickListener(new AutoPlanAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view) {
                startActivity(new Intent(getActivity(), ProductDetailsActivity.class));
            }
        });
    }

    private void initDtas() {
        for (int i = 1; i < 10; i++) {
            AutoPlanBean b = new AutoPlanBean(i + "日理财201609090" + i + "期", "7.5", i + "天  |  9919" + i + "元起", "3" + i, (i * 1 + i));
            data.add(b);
        }
    }

    private void initDta2() {
        for (int i = 11; i < 20; i++) {
            AutoPlanBean b = new AutoPlanBean(i + "日理财20160904666" + i + "期", "9.5", i + "天  |  991" + i + "元起", "3" + i, (i * 5 + i));
            data.add(b);
        }

    }
}
